﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using Telerik.Charting;
using Telerik.Charting.Styles;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BI_BGM_Request : System.Web.UI.Page
    {
        private IProjectControl controlBLL;
        private IProject projectBLL;
        private ComboBLL combo = new ComboBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            controlBLL = Project_Factory.GetProjectControlFactory("rq");
            projectBLL = Project_Factory.GetProjectFactory("rq");

            if (!IsPostBack)
            {
                LoadYear();
                LoadJobsite();
                LoadSource();
                LoadExpenseGroup();
                
                cbBgmYear.SelectedValue = Request.QueryString["year"].ToString();
                cbJobsiteSeq.SelectedValue = Request.QueryString["id"].ToString();
                LoadSubAct();
                LoadProject();
                LoadChart();
            }
        }

        public void LoadChart()
        {
            string strBgmYear = cbBgmYear.SelectedValue;
            string strJobsiteSeq = cbJobsiteSeq.SelectedValue;
            string strSubActSeq = cbSubActSeq.SelectedValue;
            string strSourceSeq = cbSourceSeq.SelectedValue;
            string strProjectSeq = cbProjectSeq.SelectedValue;
            string strExpenseGroupSeq = cbExpenseGroup.SelectedValue;


            var x = controlBLL.GetDataControlChart(strBgmYear, strSubActSeq
                , strJobsiteSeq, strProjectSeq, strSourceSeq, strExpenseGroupSeq, rblDataType.SelectedValue);

            SubActChart.DataSource = x;
            SubActChart.DataBind();

            int chartwidth = 960;
            switch (rblChartType.SelectedValue)
            {
                case "1":
                    SubActChart.DefaultType = ChartSeriesType.Pie;
                    SubActChart.Legend.Visible = true;
                    chartwidth = 960 + Convert.ToInt32(Math.Floor(Convert.ToDouble(x.Rows.Count / 12)) * 300);
                    break;
                case "2":
                    SubActChart.SeriesOrientation = ChartSeriesOrientation.Horizontal;
                    SubActChart.DefaultType = ChartSeriesType.Bar;
                    SubActChart.PlotArea.XAxis.DataLabelsColumn = "name";
                    SubActChart.Legend.Visible = false;
                    chartwidth = 960;
                    break;
            }

            SubActChart.ChartTitle.TextBlock.Text = "แผนภูมิแสดงคำของบประมาณปี " + strBgmYear + " ใน" + rblDataType.SelectedItem.Text;
             
            SubActChart.Width = System.Web.UI.WebControls.Unit.Pixel(chartwidth);
        }

        private void LoadYear()
        {
            int CurrYear = SystemInfo._CurrentYearReq;
            cbBgmYear.Items.Clear();

            for (int i = CurrYear + 5; i >= CurrYear - 5; i--)
            {
                cbBgmYear.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
            }

            cbBgmYear.SelectedValue = CurrYear.ToString();
        }

        private void LoadJobsite()
        {
            cbJobsiteSeq.DataSource = combo.ListJobsite();
            cbJobsiteSeq.DataBind();

            cbJobsiteSeq.Items.Insert(0, new RadComboBoxItem("----- ทั้งหมด -----", ""));
        }

        private void LoadSource()
        {
            cbSourceSeq.DataSource = combo.ListSourceCombo();
            cbSourceSeq.DataBind();

            cbSourceSeq.Items.Insert(0, new RadComboBoxItem("----- ทั้งหมด -----", ""));
        }

        private void LoadSubAct()
        {
            string strBgmYear = cbBgmYear.SelectedValue;

            cbSubActSeq.DataSource = combo.ListStrategy(strBgmYear);
            cbSubActSeq.DataBind();

            cbSubActSeq.Items.Insert(0, new RadComboBoxItem("----- ทั้งหมด -----", ""));
        }

        private void LoadProject()
        {
            string strBgmYear = cbBgmYear.SelectedValue;
            string strJobsiteSeq = cbJobsiteSeq.SelectedValue;
            string strSubActSeq = cbSubActSeq.SelectedValue;

            cbProjectSeq.DataSource = projectBLL.GetDataProjectByYearAndJobSiteAndSubAct(strBgmYear, strJobsiteSeq, strSubActSeq, "A");
            cbProjectSeq.DataBind();

            cbProjectSeq.Items.Insert(0, new RadComboBoxItem("----- ทั้งหมด -----", ""));
        }

        private void LoadExpenseGroup()
        {
            cbExpenseGroup.DataSource = combo.ListExpenseGroup();
            cbExpenseGroup.DataBind();

            cbExpenseGroup.Items.Insert(0, new RadComboBoxItem("----- ทั้งหมด -----", ""));
        }

        protected void Chart_ItemDataBound(object sender, ChartItemDataBoundEventArgs e)
        {
            e.SeriesItem.Name = ((DataRowView)e.DataItem)["name"].ToString();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            panelFilter.Visible = false;
            SubActChart.Visible = true;
            btnFilter.Visible = true;
            LoadChart();
        }

        protected void cbSubActSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadProject();
        }

        protected void cbBgmYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadSubAct();
            LoadProject();
        }

        protected void cbJobsiteSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadProject();
        }

        protected void btnFilter_Click(object sender, ImageClickEventArgs e)
        {
            panelFilter.Visible = true;
            SubActChart.Visible = false;
            btnFilter.Visible = false;

        }
    }
}
